unit uLibFormularioMessageBox;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, pngimage, Buttons, StdCtrls, ImgList, ActnList,
  clsMessageType;

type
  TfrmMessageBox = class(TForm)
    pnlImg: TPanel;
    pnlButtons: TPanel;
    pnlMessage: TPanel;
    img: TImage;
    mmoMessage: TMemo;
    imgLst: TImageList;
    imgLstBtn: TImageList;
    actnLst: TActionList;
    actnCopyText: TAction;
    sbCopyText: TSpeedButton;
    sbDetailMessage: TSpeedButton;
    actnDetailMessage: TAction;
    actnClose: TAction;
    sbClose: TBitBtn;
    procedure actnCloseExecute(Sender: TObject);
    procedure actnDetailMessageExecute(Sender: TObject);
    procedure actnCopyTextExecute(Sender: TObject);
  private
    FMensagem: TStringList;
    FMessageType: TMessageType;
    procedure SetMensagem(const Value: TStringList);
    procedure SetMessageType(const Value: TMessageType);
    { Private declarations }
  protected
    property Message: TStringList read FMensagem write SetMensagem;
    property MessageType: TMessageType read FMessageType write SetMessageType;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; AMessage: String;
      AMessageType: TMessageType); reintroduce; overload;
  end;

var
  frmMessageBox: TfrmMessageBox;

implementation

uses
  Clipbrd, StrUtils;

{$R *.dfm}
{ TfrmMessageBox }

procedure TfrmMessageBox.actnCloseExecute(Sender: TObject);
begin
  Self.Close;
end;

procedure TfrmMessageBox.actnCopyTextExecute(Sender: TObject);
begin
  Clipboard.AsText := Message.GetText;
end;

procedure TfrmMessageBox.actnDetailMessageExecute(Sender: TObject);
begin
  mmoMessage.Clear;
  mmoMessage.Lines.AddStrings(Message);
  actnDetailMessage.Visible := False;
end;

constructor TfrmMessageBox.Create(AOwner: TComponent; AMessage: String;
  AMessageType: TMessageType);

  function GetFriendlyMessage(AMessage: TStringList): TStringList;
  var
    Index: Integer;
  begin
    Result := TStringList.Create;
    if Pos('mensagem original:', LowerCase(AMessage[0])) <> 0 then
    begin
      Result.Add(Trim(Copy(AMessage[0], 19, 999)));
    end
    else
    begin
      Result.Add(AMessage[0]);
    end;
    for Index := 1 to AMessage.Count - 1 do
    begin
      if Pos('mensagem original:', LowerCase(AMessage[Index])) <> 0 then
      begin
        Result.Add(Trim(Copy(AMessage[Index], 19, 999)));
      end;
    end;
  end;

begin
  inherited Create(AOwner);
  Message := TStringList.Create;
  Message.Text := AMessage;
  MessageType := AMessageType;
  mmoMessage.Clear;
  mmoMessage.Lines.AddStrings(GetFriendlyMessage(Message));
  case MessageType of
    mtSucess:
      imgLst.GetIcon(0, img.Picture.Icon);
    mtInfo:
      imgLst.GetIcon(1, img.Picture.Icon);
    mtWarning:
      imgLst.GetIcon(2, img.Picture.Icon);
    mtError:
      imgLst.GetIcon(3, img.Picture.Icon);
  end;
  img.Repaint;
  img.Refresh;
  actnCopyText.Visible := MessageType <> mtSucess;
  actnDetailMessage.Visible := MessageType <> mtSucess;
end;

procedure TfrmMessageBox.SetMensagem(const Value: TStringList);
begin
  FMensagem := Value;
end;

procedure TfrmMessageBox.SetMessageType(const Value: TMessageType);
begin
  FMessageType := Value;
end;

end.
